package org.freedom.imarket.util.converter;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.core.convert.converter.Converter;

public class StringToDate implements Converter<String, Date> {

	public class NullDate extends Date {

		@Override
		public void setTime(long time) {
			super.setTime(0);
		}
		
		
	}


	/**
	 * Pattern da utilizzare per la conversione. Default: dd/MM/yyyy 
	 */
	private String pattern = "dd/MM/yyyy";
	
	@Override
	public Date convert(String source) {
		SimpleDateFormat sdf = (SimpleDateFormat) SimpleDateFormat.getDateTimeInstance();
		sdf.applyPattern(getPattern());
		
		try {
			return sdf.parse(source);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return new NullDate();
	}

	
	public static void main(String[] args) {
		String dateToFormat = "29-MAR-66 00:00:00,000000000";
		SimpleDateFormat sdf = (SimpleDateFormat) SimpleDateFormat.getDateTimeInstance();
		sdf.applyPattern("dd-MMM-yyyy HH:mm:ss,SSSSSSSSS");
		try {
			System.out.println(sdf.parse(dateToFormat));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}


	public String getPattern() {
		return pattern;
	}


	public void setPattern(String pattern) {
		this.pattern = pattern;
	}
}
